<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Silence
 * Date: 3/8/12
 * Time: 3:34 PM
 * To change this template use File | Settings | File Templates.
 */
require_once('constants.php');
function db_die($val, $msg = "")
{
    if (!$val) {
        die($msg . mysql_error());
    }

}

function db_prepare()
{
    $con = mysql_connect("localhost", DB_USERNAME, DB_PASSWORD);
    db_die($con, "Could not connect");
    mysql_select_db(DB_NAME, $con);
    return $con;
}

//function get_folders_by_parent($pid)
//{
//    $data = array();
//    switch ($pid) {
//        case ROOT_FID:
//            $data[] = array(
//                'id' => MYFOLDER_FID,
//                'fid' => MYFOLDER_FID,
//                'pid' => MYFOLDER_FID,
//                'text' => "My Folders",
//                'status' => STATUS_ACTIVE,
//                'expanded' => true,
//                'children' => array(),
//            );
//            $data[] = array(
//                'id' => RECYCLE_BIN_FID,
//                'fid' => RECYCLE_BIN_FID,
//                'pid' => RECYCLE_BIN_FID,
//                'text' => "Recycle Bin",
//                'status' => STATUS_ACTIVE,
//                'iconCls' => 'recyclebin x-tree-node-icon',
//                'children' => array(),
//            );
//            return $data;
//            break;
//        case MYFOLDER_FID:
//            $sql = "SELECT fid, pid, name, status FROM `folder` WHERE pid = 0 AND status = " . STATUS_ACTIVE;
//            break;
//        case RECYCLE_BIN_FID:
//            $sql = "SELECT fid, pid, name, status FROM `folder` WHERE status = " . STATUS_INACTIVE;
//            break;
//        default:
//            $sql = "SELECT fid, pid, name, status FROM `folder` WHERE pid = $pid AND status = " . STATUS_ACTIVE;
//            break;
//    }
//    db_die($result = mysql_query($sql));
//    while ($row = mysql_fetch_array($result)) {
//        $data[] = array(
//            'id' => $row['fid'],
//            'fid' => $row['fid'],
//            'pid' => $row['pid'],
//            'text' => $row['name'],
//            'status' => $row['status'],
//        );
//    }
//    return $data;
//}
//
//function get_folders_by_name($name)
//{
//    $sql = "SELECT fid, pid, name, status FROM `folder` WHERE name LIKE '%$name%'";
//    db_die($result = mysql_query($sql));
//    while ($row = mysql_fetch_array($result)) {
//        $data[] = array(
//            'id' => $row['fid'],
//            'fid' => $row['fid'],
//            'pid' => $row['pid'],
//            'text' => $row['name'],
//            'status' => $row['status'],
//            'children' => array(),
//        );
//    }
//    return $data;
//}
//
//function get_folders()
//{
//    $sql = "SELECT fid, pid, name, status FROM `folder` WHERE 1";
//    db_die($result = mysql_query($sql));
//
//    $raw_data = array();
//    $raw_data[MYFOLDER_FID] = array(
//        'text' => 'My Folders',
//        'id' => MYFOLDER_FID,
//        'fid' => MYFOLDER_FID,
//        'pid' => MYFOLDER_FID,
//        'status' => STATUS_ACTIVE,
//    );
//
//    $raw_data[RECYCLE_BIN_FID] = array(
//        'text' => 'Recycle Bin',
//        'id' => RECYCLE_BIN_FID,
//        'fid' => RECYCLE_BIN_FID,
//        'pid' => RECYCLE_BIN_FID,
//        'status' => STATUS_ACTIVE,
//    );
//
//
//    while ($row = mysql_fetch_array($result)) {
//        if ($row['status'] == STATUS_ACTIVE) {
//            $raw_data[$row['pid']]['children'][] = $row['fid'];
//        } else if ($row['status'] == STATUS_INACTIVE) {
//            $raw_data[RECYCLE_BIN_FID]['children'][] = $row['fid'];
//        }
//
//        $raw_data[$row['fid']]['text'] = $row['name'];
//        $raw_data[$row['fid']]['pid'] = $row['pid'];
//        $raw_data[$row['fid']]['status'] = $row['status'];
//        $raw_data[$row['fid']]['fid'] = $row['fid'];
//    }
//
//    return $raw_data;
//}
//
//function build_folder_tree($raw_data)
//{
//    $data[0] = build_folder_tree_recursive(MYFOLDER_FID, $raw_data, array());
//    $data[0]['expanded'] = true;
//    $data[1] = build_folder_tree_recursive(RECYCLE_BIN_FID, $raw_data, array());
//    $data[1]['iconCls'] = 'recyclebin x-tree-node-icon';
//    $data[0]['expanded'] = true;
//    return $data;
//}
//
//function build_folder_tree_recursive($fid, &$raw_data, $settings)
//{
//    $data = array();
//    $data['text'] = $raw_data[$fid]['text'];
//    $data['id'] = $data['fid'] = $raw_data[$fid]['fid'];
//    $data['pid'] = $raw_data[$fid]['pid'];
//    $data['status'] = $raw_data[$fid]['status'];
//    $data['children'] = array();
//
//    $data = array_merge($data, $settings);
//
//    if (isset($raw_data[$fid]['children'])) {
//        foreach ($raw_data[$fid]['children'] as $child_fid) {
//            $data['children'][] = build_folder_tree_recursive($child_fid, $raw_data, $settings);
//        }
//    }
//    return $data;
//}
//
//
//function build_event_tree()
//{
//    $sql = "SELECT * FROM `event` INNER JOIN `event_event` ON event.id = event_event.cid WHERE status = 1";
//    $data = array();
//    db_die($result = mysql_query($sql));
//    while ($row = mysql_fetch_assoc($result)) {
//        //        print "<pre>";
//        //        print_r ($row);
//        //        print "</pre>";
//        $data[$row['id']]['Status'] = $row['status'];
//        $data[$row['id']]['Id'] = $row['id'];
//        $data[$row['id']]['Name'] = $row['name'];
//        $data[$row['id']]['StartDate'] = $row['start_date'];
//        $data[$row['id']]['EndDate'] = $row['end_date'];
//        $data[$row['pid']]['children_ids'][] = $row['id'];
//        $data[$row['id']]['dirty'] = true;
//    }
//    $tree = array();
//    //    print_r($data);
//    $data[0]['Id'] = 0;
//    foreach ($data as $node) {
//        if (!isset($node['dirty'])) {
//            $tree[] = build_event_tree_recursive($node['Id'], $data);
//        }
//    }
//    return $tree[0]['children'];
//}
//
//function build_event_tree_recursive($id, $data)
//{
//    if (isset($data[$id]['children_ids'])) {
//        foreach ($data[$id]['children_ids'] as $child_id) {
//            $data[$id]['children'][] = build_event_tree_recursive($child_id, $data);
//        }
//    } else {
//        $data[$id]['leaf'] = true;
//    }
//    unset($data[$id]['children_ids']);
//    unset($data[$id]['dirty']);
//    return $data[$id];
//}
//
//function delete_folder($data)
//{
//    $sql = "DELETE from `folder` where `fid` = $data->id";
//    return mysql_query($sql);
//}
//
//function update_folder($data)
//{
//    $sql = "UPDATE `folder` SET `name` = '$data->text', `status` = $data->status,
//    `pid` = $data->pid
//     WHERE `fid` = $data->id";
//    return mysql_query($sql) ? true : false;
//}
//
//function add_folder($data)
//{
//    $sql = "INSERT INTO `folder` (pid, name) VALUES ($data->parentId, '$data->text')";
//    if (mysql_query($sql)) {
//        $data->pid = $data->parentId;
//        $data->status = STATUS_ACTIVE;
//        $data->id = mysql_insert_id();
//        return $data;
//    } else {
//        return false;
//    }
//}

function get_params_from_raw_http_request()
{
    $raw = '';
    $httpContent = fopen('php://input', 'r');
    while ($kb = fread($httpContent, 1024)) {
        $raw .= $kb;
    }
    fclose($httpContent);
    $param = array();
    parse_str($raw, $param);

    if (isset($param['data'])) {
        $param = json_decode(stripslashes($param['data']));
    } else {
        $param = json_decode(stripslashes($raw));
    }

    return $param;
}

function check_attr($obj = null, $attrs = null)
{
    if ($obj === null)
        throw new ErrorException("Null object.");

    if ($attrs) {
        if (!is_array($attrs))
            $attrs = array($attrs);

        foreach ($attrs as $attr) {
            if (!isset($obj->$attr))
                throw new ErrorException("No attribute '$attr'.");
        }
    }
}

function get_keys_values_pair($data, $keys)
{
    $ks = array();
    $vs = array();
    foreach ($keys as $k)
    {
        if (isset($data->$k)) {
            $ks[] = $k;
            $vs[] = $data->$k;
        }
    }
    return array($ks, $vs);
}

function get_key_value_pairs($data, $keys)
{
    $pairs = array();
    foreach ($keys as $k)
    {
        if (isset($data->$k)) {
            $v = $data->$k;
            $pairs[] = "`$k`='$v'";
        }
    }
    return implode(',', $pairs);
}